<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>linkbubble.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_plugins_linkbubble.js.html">linkbubble.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Base class for bubble plugins.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.editor.plugins.LinkBubble&#39;);
<a name="line21"></a>goog.provide(&#39;goog.editor.plugins.LinkBubble.Action&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.editor.BrowserFeature&#39;);
<a name="line26"></a>goog.require(&#39;goog.editor.Command&#39;);
<a name="line27"></a>goog.require(&#39;goog.editor.Link&#39;);
<a name="line28"></a>goog.require(&#39;goog.editor.plugins.AbstractBubblePlugin&#39;);
<a name="line29"></a>goog.require(&#39;goog.editor.range&#39;);
<a name="line30"></a>goog.require(&#39;goog.string&#39;);
<a name="line31"></a>goog.require(&#39;goog.style&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.editor.messages&#39;);
<a name="line33"></a>goog.require(&#39;goog.uri.utils&#39;);
<a name="line34"></a>goog.require(&#39;goog.window&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Property bubble plugin for links.
<a name="line40"></a> * @param {...!goog.editor.plugins.LinkBubble.Action} var_args List of
<a name="line41"></a> *     extra actions supported by the bubble.
<a name="line42"></a> * @constructor
<a name="line43"></a> * @extends {goog.editor.plugins.AbstractBubblePlugin}
<a name="line44"></a> */
<a name="line45"></a>goog.editor.plugins.LinkBubble = function(var_args) {
<a name="line46"></a>  goog.editor.plugins.LinkBubble.base(this, &#39;constructor&#39;);
<a name="line47"></a>
<a name="line48"></a>  /**
<a name="line49"></a>   * List of extra actions supported by the bubble.
<a name="line50"></a>   * @type {Array.&lt;!goog.editor.plugins.LinkBubble.Action&gt;}
<a name="line51"></a>   * @private
<a name="line52"></a>   */
<a name="line53"></a>  this.extraActions_ = goog.array.toArray(arguments);
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * List of spans corresponding to the extra actions.
<a name="line57"></a>   * @type {Array.&lt;!Element&gt;}
<a name="line58"></a>   * @private
<a name="line59"></a>   */
<a name="line60"></a>  this.actionSpans_ = [];
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * A list of whitelisted URL schemes which are safe to open.
<a name="line64"></a>   * @type {Array.&lt;string&gt;}
<a name="line65"></a>   * @private
<a name="line66"></a>   */
<a name="line67"></a>  this.safeToOpenSchemes_ = [&#39;http&#39;, &#39;https&#39;, &#39;ftp&#39;];
<a name="line68"></a>};
<a name="line69"></a>goog.inherits(goog.editor.plugins.LinkBubble,
<a name="line70"></a>    goog.editor.plugins.AbstractBubblePlugin);
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * Element id for the link text.
<a name="line75"></a> * type {string}
<a name="line76"></a> * @private
<a name="line77"></a> */
<a name="line78"></a>goog.editor.plugins.LinkBubble.LINK_TEXT_ID_ = &#39;tr_link-text&#39;;
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Element id for the test link span.
<a name="line83"></a> * type {string}
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.editor.plugins.LinkBubble.TEST_LINK_SPAN_ID_ = &#39;tr_test-link-span&#39;;
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Element id for the test link.
<a name="line91"></a> * type {string}
<a name="line92"></a> * @private
<a name="line93"></a> */
<a name="line94"></a>goog.editor.plugins.LinkBubble.TEST_LINK_ID_ = &#39;tr_test-link&#39;;
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Element id for the change link span.
<a name="line99"></a> * type {string}
<a name="line100"></a> * @private
<a name="line101"></a> */
<a name="line102"></a>goog.editor.plugins.LinkBubble.CHANGE_LINK_SPAN_ID_ = &#39;tr_change-link-span&#39;;
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Element id for the link.
<a name="line107"></a> * type {string}
<a name="line108"></a> * @private
<a name="line109"></a> */
<a name="line110"></a>goog.editor.plugins.LinkBubble.CHANGE_LINK_ID_ = &#39;tr_change-link&#39;;
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Element id for the delete link span.
<a name="line115"></a> * type {string}
<a name="line116"></a> * @private
<a name="line117"></a> */
<a name="line118"></a>goog.editor.plugins.LinkBubble.DELETE_LINK_SPAN_ID_ = &#39;tr_delete-link-span&#39;;
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Element id for the delete link.
<a name="line123"></a> * type {string}
<a name="line124"></a> * @private
<a name="line125"></a> */
<a name="line126"></a>goog.editor.plugins.LinkBubble.DELETE_LINK_ID_ = &#39;tr_delete-link&#39;;
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Element id for the link bubble wrapper div.
<a name="line131"></a> * type {string}
<a name="line132"></a> * @private
<a name="line133"></a> */
<a name="line134"></a>goog.editor.plugins.LinkBubble.LINK_DIV_ID_ = &#39;tr_link-div&#39;;
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * @desc Text label for link that lets the user click it to see where the link
<a name="line139"></a> *     this bubble is for point to.
<a name="line140"></a> */
<a name="line141"></a>var MSG_LINK_BUBBLE_TEST_LINK = goog.getMsg(&#39;Go to link: &#39;);
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/**
<a name="line145"></a> * @desc Label that pops up a dialog to change the link.
<a name="line146"></a> */
<a name="line147"></a>var MSG_LINK_BUBBLE_CHANGE = goog.getMsg(&#39;Change&#39;);
<a name="line148"></a>
<a name="line149"></a>
<a name="line150"></a>/**
<a name="line151"></a> * @desc Label that allow the user to remove this link.
<a name="line152"></a> */
<a name="line153"></a>var MSG_LINK_BUBBLE_REMOVE = goog.getMsg(&#39;Remove&#39;);
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Whether to stop leaking the page&#39;s url via the referrer header when the
<a name="line158"></a> * link text link is clicked.
<a name="line159"></a> * @type {boolean}
<a name="line160"></a> * @private
<a name="line161"></a> */
<a name="line162"></a>goog.editor.plugins.LinkBubble.prototype.stopReferrerLeaks_ = false;
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Whether to block opening links with a non-whitelisted URL scheme.
<a name="line167"></a> * @type {boolean}
<a name="line168"></a> * @private
<a name="line169"></a> */
<a name="line170"></a>goog.editor.plugins.LinkBubble.prototype.blockOpeningUnsafeSchemes_ =
<a name="line171"></a>    true;
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Tells the plugin to stop leaking the page&#39;s url via the referrer header when
<a name="line176"></a> * the link text link is clicked. When the user clicks on a link, the
<a name="line177"></a> * browser makes a request for the link url, passing the url of the current page
<a name="line178"></a> * in the request headers. If the user wants the current url to be kept secret
<a name="line179"></a> * (e.g. an unpublished document), the owner of the url that was clicked will
<a name="line180"></a> * see the secret url in the request headers, and it will no longer be a secret.
<a name="line181"></a> * Calling this method will not send a referrer header in the request, just as
<a name="line182"></a> * if the user had opened a blank window and typed the url in themselves.
<a name="line183"></a> */
<a name="line184"></a>goog.editor.plugins.LinkBubble.prototype.stopReferrerLeaks = function() {
<a name="line185"></a>  // TODO(user): Right now only 2 plugins have this API to stop
<a name="line186"></a>  // referrer leaks. If more plugins need to do this, come up with a way to
<a name="line187"></a>  // enable the functionality in all plugins at once. Same thing for
<a name="line188"></a>  // setBlockOpeningUnsafeSchemes and associated functionality.
<a name="line189"></a>  this.stopReferrerLeaks_ = true;
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * Tells the plugin whether to block URLs with schemes not in the whitelist.
<a name="line195"></a> * If blocking is enabled, this plugin will not linkify the link in the bubble
<a name="line196"></a> * popup.
<a name="line197"></a> * @param {boolean} blockOpeningUnsafeSchemes Whether to block non-whitelisted
<a name="line198"></a> *     schemes.
<a name="line199"></a> */
<a name="line200"></a>goog.editor.plugins.LinkBubble.prototype.setBlockOpeningUnsafeSchemes =
<a name="line201"></a>    function(blockOpeningUnsafeSchemes) {
<a name="line202"></a>  this.blockOpeningUnsafeSchemes_ = blockOpeningUnsafeSchemes;
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Sets a whitelist of allowed URL schemes that are safe to open.
<a name="line208"></a> * Schemes should all be in lowercase. If the plugin is set to block opening
<a name="line209"></a> * unsafe schemes, user-entered URLs will be converted to lowercase and checked
<a name="line210"></a> * against this list. The whitelist has no effect if blocking is not enabled.
<a name="line211"></a> * @param {Array.&lt;string&gt;} schemes String array of URL schemes to allow (http,
<a name="line212"></a> *     https, etc.).
<a name="line213"></a> */
<a name="line214"></a>goog.editor.plugins.LinkBubble.prototype.setSafeToOpenSchemes =
<a name="line215"></a>    function(schemes) {
<a name="line216"></a>  this.safeToOpenSchemes_ = schemes;
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/** @override */
<a name="line221"></a>goog.editor.plugins.LinkBubble.prototype.getTrogClassId = function() {
<a name="line222"></a>  return &#39;LinkBubble&#39;;
<a name="line223"></a>};
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/** @override */
<a name="line227"></a>goog.editor.plugins.LinkBubble.prototype.isSupportedCommand =
<a name="line228"></a>    function(command) {
<a name="line229"></a>  return command == goog.editor.Command.UPDATE_LINK_BUBBLE;
<a name="line230"></a>};
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/** @override */
<a name="line234"></a>goog.editor.plugins.LinkBubble.prototype.execCommandInternal =
<a name="line235"></a>    function(command, var_args) {
<a name="line236"></a>  if (command == goog.editor.Command.UPDATE_LINK_BUBBLE) {
<a name="line237"></a>    this.updateLink_();
<a name="line238"></a>  }
<a name="line239"></a>};
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Updates the href in the link bubble with a new link.
<a name="line244"></a> * @private
<a name="line245"></a> */
<a name="line246"></a>goog.editor.plugins.LinkBubble.prototype.updateLink_ = function() {
<a name="line247"></a>  var targetEl = this.getTargetElement();
<a name="line248"></a>  this.closeBubble();
<a name="line249"></a>  this.createBubble(targetEl);
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/** @override */
<a name="line254"></a>goog.editor.plugins.LinkBubble.prototype.getBubbleTargetFromSelection =
<a name="line255"></a>    function(selectedElement) {
<a name="line256"></a>  var bubbleTarget = goog.dom.getAncestorByTagNameAndClass(selectedElement,
<a name="line257"></a>      goog.dom.TagName.A);
<a name="line258"></a>
<a name="line259"></a>  if (!bubbleTarget) {
<a name="line260"></a>    // See if the selection is touching the right side of a link, and if so,
<a name="line261"></a>    // show a bubble for that link.  The check for &quot;touching&quot; is very brittle,
<a name="line262"></a>    // and currently only guarantees that it will pop up a bubble at the
<a name="line263"></a>    // position the cursor is placed at after the link dialog is closed.
<a name="line264"></a>    // NOTE(robbyw): This assumes this method is always called with
<a name="line265"></a>    // selected element = range.getContainerElement().  Right now this is true,
<a name="line266"></a>    // but attempts to re-use this method for other purposes could cause issues.
<a name="line267"></a>    // TODO(robbyw): Refactor this method to also take a range, and use that.
<a name="line268"></a>    var range = this.getFieldObject().getRange();
<a name="line269"></a>    if (range &amp;&amp; range.isCollapsed() &amp;&amp; range.getStartOffset() == 0) {
<a name="line270"></a>      var startNode = range.getStartNode();
<a name="line271"></a>      var previous = startNode.previousSibling;
<a name="line272"></a>      if (previous &amp;&amp; previous.tagName == goog.dom.TagName.A) {
<a name="line273"></a>        bubbleTarget = previous;
<a name="line274"></a>      }
<a name="line275"></a>    }
<a name="line276"></a>  }
<a name="line277"></a>
<a name="line278"></a>  return /** @type {Element} */ (bubbleTarget);
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Set the optional function for getting the &quot;test&quot; link of a url.
<a name="line284"></a> * @param {function(string) : string} func The function to use.
<a name="line285"></a> */
<a name="line286"></a>goog.editor.plugins.LinkBubble.prototype.setTestLinkUrlFn = function(func) {
<a name="line287"></a>  this.testLinkUrlFn_ = func;
<a name="line288"></a>};
<a name="line289"></a>
<a name="line290"></a>
<a name="line291"></a>/**
<a name="line292"></a> * Returns the target element url for the bubble.
<a name="line293"></a> * @return {string} The url href.
<a name="line294"></a> * @protected
<a name="line295"></a> */
<a name="line296"></a>goog.editor.plugins.LinkBubble.prototype.getTargetUrl = function() {
<a name="line297"></a>  // Get the href-attribute through getAttribute() rather than the href property
<a name="line298"></a>  // because Google-Toolbar on Firefox with &quot;Send with Gmail&quot; turned on
<a name="line299"></a>  // modifies the href-property of &#39;mailto:&#39; links but leaves the attribute
<a name="line300"></a>  // untouched.
<a name="line301"></a>  return this.getTargetElement().getAttribute(&#39;href&#39;) || &#39;&#39;;
<a name="line302"></a>};
<a name="line303"></a>
<a name="line304"></a>
<a name="line305"></a>/** @override */
<a name="line306"></a>goog.editor.plugins.LinkBubble.prototype.getBubbleType = function() {
<a name="line307"></a>  return goog.dom.TagName.A;
<a name="line308"></a>};
<a name="line309"></a>
<a name="line310"></a>
<a name="line311"></a>/** @override */
<a name="line312"></a>goog.editor.plugins.LinkBubble.prototype.getBubbleTitle = function() {
<a name="line313"></a>  return goog.ui.editor.messages.MSG_LINK_CAPTION;
<a name="line314"></a>};
<a name="line315"></a>
<a name="line316"></a>
<a name="line317"></a>/** @override */
<a name="line318"></a>goog.editor.plugins.LinkBubble.prototype.createBubbleContents = function(
<a name="line319"></a>    bubbleContainer) {
<a name="line320"></a>  var linkObj = this.getLinkToTextObj_();
<a name="line321"></a>
<a name="line322"></a>  // Create linkTextSpan, show plain text for e-mail address or truncate the
<a name="line323"></a>  // text to &lt;= 48 characters so that property bubbles don&#39;t grow too wide and
<a name="line324"></a>  // create a link if URL.  Only linkify valid links.
<a name="line325"></a>  // TODO(robbyw): Repalce this color with a CSS class.
<a name="line326"></a>  var color = linkObj.valid ? &#39;black&#39; : &#39;red&#39;;
<a name="line327"></a>  var shouldOpenUrl = this.shouldOpenUrl(linkObj.linkText);
<a name="line328"></a>  var linkTextSpan;
<a name="line329"></a>  if (goog.editor.Link.isLikelyEmailAddress(linkObj.linkText) ||
<a name="line330"></a>      !linkObj.valid || !shouldOpenUrl) {
<a name="line331"></a>    linkTextSpan = this.dom_.createDom(goog.dom.TagName.SPAN,
<a name="line332"></a>        {
<a name="line333"></a>          id: goog.editor.plugins.LinkBubble.LINK_TEXT_ID_,
<a name="line334"></a>          style: &#39;color:&#39; + color
<a name="line335"></a>        }, this.dom_.createTextNode(linkObj.linkText));
<a name="line336"></a>  } else {
<a name="line337"></a>    var testMsgSpan = this.dom_.createDom(goog.dom.TagName.SPAN,
<a name="line338"></a>        {id: goog.editor.plugins.LinkBubble.TEST_LINK_SPAN_ID_},
<a name="line339"></a>        MSG_LINK_BUBBLE_TEST_LINK);
<a name="line340"></a>    linkTextSpan = this.dom_.createDom(goog.dom.TagName.SPAN,
<a name="line341"></a>        {
<a name="line342"></a>          id: goog.editor.plugins.LinkBubble.LINK_TEXT_ID_,
<a name="line343"></a>          style: &#39;color:&#39; + color
<a name="line344"></a>        }, &#39;&#39;);
<a name="line345"></a>    var linkText = goog.string.truncateMiddle(linkObj.linkText, 48);
<a name="line346"></a>    // Actually creates a pseudo-link that can&#39;t be right-clicked to open in a
<a name="line347"></a>    // new tab, because that would avoid the logic to stop referrer leaks.
<a name="line348"></a>    this.createLink(goog.editor.plugins.LinkBubble.TEST_LINK_ID_,
<a name="line349"></a>                    this.dom_.createTextNode(linkText).data,
<a name="line350"></a>                    this.testLink,
<a name="line351"></a>                    linkTextSpan);
<a name="line352"></a>  }
<a name="line353"></a>
<a name="line354"></a>  var changeLinkSpan = this.createLinkOption(
<a name="line355"></a>      goog.editor.plugins.LinkBubble.CHANGE_LINK_SPAN_ID_);
<a name="line356"></a>  this.createLink(goog.editor.plugins.LinkBubble.CHANGE_LINK_ID_,
<a name="line357"></a>      MSG_LINK_BUBBLE_CHANGE, this.showLinkDialog_, changeLinkSpan);
<a name="line358"></a>
<a name="line359"></a>  // This function is called multiple times - we have to reset the array.
<a name="line360"></a>  this.actionSpans_ = [];
<a name="line361"></a>  for (var i = 0; i &lt; this.extraActions_.length; i++) {
<a name="line362"></a>    var action = this.extraActions_[i];
<a name="line363"></a>    var actionSpan = this.createLinkOption(action.spanId_);
<a name="line364"></a>    this.actionSpans_.push(actionSpan);
<a name="line365"></a>    this.createLink(action.linkId_, action.message_,
<a name="line366"></a>        function() {
<a name="line367"></a>          action.actionFn_(this.getTargetUrl());
<a name="line368"></a>        },
<a name="line369"></a>        actionSpan);
<a name="line370"></a>  }
<a name="line371"></a>
<a name="line372"></a>  var removeLinkSpan = this.createLinkOption(
<a name="line373"></a>      goog.editor.plugins.LinkBubble.DELETE_LINK_SPAN_ID_);
<a name="line374"></a>  this.createLink(goog.editor.plugins.LinkBubble.DELETE_LINK_ID_,
<a name="line375"></a>      MSG_LINK_BUBBLE_REMOVE, this.deleteLink_, removeLinkSpan);
<a name="line376"></a>
<a name="line377"></a>  this.onShow();
<a name="line378"></a>
<a name="line379"></a>  var bubbleContents = this.dom_.createDom(goog.dom.TagName.DIV,
<a name="line380"></a>      {id: goog.editor.plugins.LinkBubble.LINK_DIV_ID_},
<a name="line381"></a>      testMsgSpan || &#39;&#39;, linkTextSpan, changeLinkSpan);
<a name="line382"></a>
<a name="line383"></a>  for (i = 0; i &lt; this.actionSpans_.length; i++) {
<a name="line384"></a>    bubbleContents.appendChild(this.actionSpans_[i]);
<a name="line385"></a>  }
<a name="line386"></a>  bubbleContents.appendChild(removeLinkSpan);
<a name="line387"></a>
<a name="line388"></a>  goog.dom.appendChild(bubbleContainer, bubbleContents);
<a name="line389"></a>};
<a name="line390"></a>
<a name="line391"></a>
<a name="line392"></a>/**
<a name="line393"></a> * Tests the link by opening it in a new tab/window. Should be used as the
<a name="line394"></a> * click event handler for the test pseudo-link.
<a name="line395"></a> * @protected
<a name="line396"></a> */
<a name="line397"></a>goog.editor.plugins.LinkBubble.prototype.testLink = function() {
<a name="line398"></a>  goog.window.open(this.getTestLinkAction_(),
<a name="line399"></a>      {
<a name="line400"></a>        &#39;target&#39;: &#39;_blank&#39;,
<a name="line401"></a>        &#39;noreferrer&#39;: this.stopReferrerLeaks_
<a name="line402"></a>      }, this.getFieldObject().getAppWindow());
<a name="line403"></a>};
<a name="line404"></a>
<a name="line405"></a>
<a name="line406"></a>/**
<a name="line407"></a> * Returns whether the URL should be considered invalid.  This always returns
<a name="line408"></a> * false in the base class, and should be overridden by subclasses that wish
<a name="line409"></a> * to impose validity rules on URLs.
<a name="line410"></a> * @param {string} url The url to check.
<a name="line411"></a> * @return {boolean} Whether the URL should be considered invalid.
<a name="line412"></a> */
<a name="line413"></a>goog.editor.plugins.LinkBubble.prototype.isInvalidUrl = goog.functions.FALSE;
<a name="line414"></a>
<a name="line415"></a>
<a name="line416"></a>/**
<a name="line417"></a> * Gets the text to display for a link, based on the type of link
<a name="line418"></a> * @return {!Object} Returns an object of the form:
<a name="line419"></a> *     {linkText: displayTextForLinkTarget, valid: ifTheLinkIsValid}.
<a name="line420"></a> * @private
<a name="line421"></a> */
<a name="line422"></a>goog.editor.plugins.LinkBubble.prototype.getLinkToTextObj_ = function() {
<a name="line423"></a>  var isError;
<a name="line424"></a>  var targetUrl = this.getTargetUrl();
<a name="line425"></a>
<a name="line426"></a>  if (this.isInvalidUrl(targetUrl)) {
<a name="line427"></a>    /**
<a name="line428"></a>     * @desc Message shown in a link bubble when the link is not a valid url.
<a name="line429"></a>     */
<a name="line430"></a>    var MSG_INVALID_URL_LINK_BUBBLE = goog.getMsg(&#39;invalid url&#39;);
<a name="line431"></a>    targetUrl = MSG_INVALID_URL_LINK_BUBBLE;
<a name="line432"></a>    isError = true;
<a name="line433"></a>  } else if (goog.editor.Link.isMailto(targetUrl)) {
<a name="line434"></a>    targetUrl = targetUrl.substring(7); // 7 == &quot;mailto:&quot;.length
<a name="line435"></a>  }
<a name="line436"></a>
<a name="line437"></a>  return {linkText: targetUrl, valid: !isError};
<a name="line438"></a>};
<a name="line439"></a>
<a name="line440"></a>
<a name="line441"></a>/**
<a name="line442"></a> * Shows the link dialog.
<a name="line443"></a> * @param {goog.events.BrowserEvent} e The event.
<a name="line444"></a> * @private
<a name="line445"></a> */
<a name="line446"></a>goog.editor.plugins.LinkBubble.prototype.showLinkDialog_ = function(e) {
<a name="line447"></a>  // Needed when this occurs due to an ENTER key event, else the newly created
<a name="line448"></a>  // dialog manages to have its OK button pressed, causing it to disappear.
<a name="line449"></a>  e.preventDefault();
<a name="line450"></a>
<a name="line451"></a>  this.getFieldObject().execCommand(goog.editor.Command.MODAL_LINK_EDITOR,
<a name="line452"></a>      new goog.editor.Link(
<a name="line453"></a>          /** @type {HTMLAnchorElement} */ (this.getTargetElement()),
<a name="line454"></a>          false));
<a name="line455"></a>  this.closeBubble();
<a name="line456"></a>};
<a name="line457"></a>
<a name="line458"></a>
<a name="line459"></a>/**
<a name="line460"></a> * Deletes the link associated with the bubble
<a name="line461"></a> * @private
<a name="line462"></a> */
<a name="line463"></a>goog.editor.plugins.LinkBubble.prototype.deleteLink_ = function() {
<a name="line464"></a>  this.getFieldObject().dispatchBeforeChange();
<a name="line465"></a>
<a name="line466"></a>  var link = this.getTargetElement();
<a name="line467"></a>  var child = link.lastChild;
<a name="line468"></a>  goog.dom.flattenElement(link);
<a name="line469"></a>  goog.editor.range.placeCursorNextTo(child, false);
<a name="line470"></a>
<a name="line471"></a>  this.closeBubble();
<a name="line472"></a>
<a name="line473"></a>  this.getFieldObject().dispatchChange();
<a name="line474"></a>  this.getFieldObject().focus();
<a name="line475"></a>};
<a name="line476"></a>
<a name="line477"></a>
<a name="line478"></a>/**
<a name="line479"></a> * Sets the proper state for the action links.
<a name="line480"></a> * @protected
<a name="line481"></a> * @override
<a name="line482"></a> */
<a name="line483"></a>goog.editor.plugins.LinkBubble.prototype.onShow = function() {
<a name="line484"></a>  var linkDiv = this.dom_.getElement(
<a name="line485"></a>      goog.editor.plugins.LinkBubble.LINK_DIV_ID_);
<a name="line486"></a>  if (linkDiv) {
<a name="line487"></a>    var testLinkSpan = this.dom_.getElement(
<a name="line488"></a>        goog.editor.plugins.LinkBubble.TEST_LINK_SPAN_ID_);
<a name="line489"></a>    if (testLinkSpan) {
<a name="line490"></a>      var url = this.getTargetUrl();
<a name="line491"></a>      goog.style.setElementShown(testLinkSpan, !goog.editor.Link.isMailto(url));
<a name="line492"></a>    }
<a name="line493"></a>
<a name="line494"></a>    for (var i = 0; i &lt; this.extraActions_.length; i++) {
<a name="line495"></a>      var action = this.extraActions_[i];
<a name="line496"></a>      var actionSpan = this.dom_.getElement(action.spanId_);
<a name="line497"></a>      if (actionSpan) {
<a name="line498"></a>        goog.style.setElementShown(actionSpan, action.toShowFn_(
<a name="line499"></a>            this.getTargetUrl()));
<a name="line500"></a>      }
<a name="line501"></a>    }
<a name="line502"></a>  }
<a name="line503"></a>};
<a name="line504"></a>
<a name="line505"></a>
<a name="line506"></a>/**
<a name="line507"></a> * Gets the url for the bubble test link.  The test link is the link in the
<a name="line508"></a> * bubble the user can click on to make sure the link they entered is correct.
<a name="line509"></a> * @return {string} The url for the bubble link href.
<a name="line510"></a> * @private
<a name="line511"></a> */
<a name="line512"></a>goog.editor.plugins.LinkBubble.prototype.getTestLinkAction_ = function() {
<a name="line513"></a>  var targetUrl = this.getTargetUrl();
<a name="line514"></a>  return this.testLinkUrlFn_ ? this.testLinkUrlFn_(targetUrl) : targetUrl;
<a name="line515"></a>};
<a name="line516"></a>
<a name="line517"></a>
<a name="line518"></a>/**
<a name="line519"></a> * Checks whether the plugin should open the given url in a new window.
<a name="line520"></a> * @param {string} url The url to check.
<a name="line521"></a> * @return {boolean} If the plugin should open the given url in a new window.
<a name="line522"></a> * @protected
<a name="line523"></a> */
<a name="line524"></a>goog.editor.plugins.LinkBubble.prototype.shouldOpenUrl = function(url) {
<a name="line525"></a>  return !this.blockOpeningUnsafeSchemes_ || this.isSafeSchemeToOpen_(url);
<a name="line526"></a>};
<a name="line527"></a>
<a name="line528"></a>
<a name="line529"></a>/**
<a name="line530"></a> * Determines whether or not a url has a scheme which is safe to open.
<a name="line531"></a> * Schemes like javascript are unsafe due to the possibility of XSS.
<a name="line532"></a> * @param {string} url A url.
<a name="line533"></a> * @return {boolean} Whether the url has a safe scheme.
<a name="line534"></a> * @private
<a name="line535"></a> */
<a name="line536"></a>goog.editor.plugins.LinkBubble.prototype.isSafeSchemeToOpen_ =
<a name="line537"></a>    function(url) {
<a name="line538"></a>  var scheme = goog.uri.utils.getScheme(url) || &#39;http&#39;;
<a name="line539"></a>  return goog.array.contains(this.safeToOpenSchemes_, scheme.toLowerCase());
<a name="line540"></a>};
<a name="line541"></a>
<a name="line542"></a>
<a name="line543"></a>
<a name="line544"></a>/**
<a name="line545"></a> * Constructor for extra actions that can be added to the link bubble.
<a name="line546"></a> * @param {string} spanId The ID for the span showing the action.
<a name="line547"></a> * @param {string} linkId The ID for the link showing the action.
<a name="line548"></a> * @param {string} message The text for the link showing the action.
<a name="line549"></a> * @param {function(string):boolean} toShowFn Test function to determine whether
<a name="line550"></a> *     to show the action for the given URL.
<a name="line551"></a> * @param {function(string):void} actionFn Action function to run when the
<a name="line552"></a> *     action is clicked.  Takes the current target URL as a parameter.
<a name="line553"></a> * @constructor
<a name="line554"></a> * @final
<a name="line555"></a> */
<a name="line556"></a>goog.editor.plugins.LinkBubble.Action = function(spanId, linkId, message,
<a name="line557"></a>    toShowFn, actionFn) {
<a name="line558"></a>  this.spanId_ = spanId;
<a name="line559"></a>  this.linkId_ = linkId;
<a name="line560"></a>  this.message_ = message;
<a name="line561"></a>  this.toShowFn_ = toShowFn;
<a name="line562"></a>  this.actionFn_ = actionFn;
<a name="line563"></a>};
</pre>


</body>
</html>
